<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>实例化instancing3</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
    }
    canvas {
      outline: none;
    }
  </style>
</head>
<body>
  <script type="x-shader/x-vertex" id="vertexShader">
		precision highp float;

    uniform float time;

    attribute vec3 translate;
    
    varying vec2 vUv;
    varying float vScale;

    void main() {
      vec4 vPosition = modelViewMatrix * vec4(translate, 1);
      vec3 trTime = vec3(translate.x + time, translate.y + time, translate.z + time); 
      float scale = sin(trTime.x * 2.1) + sin(trTime.y * 3.2) + sin(trTime.z * 4.3);
      vScale = scale;
      scale = scale * 10.0 + 10.0;
      vPosition.xyz += position * scale;
      vUv = uv;
      gl_Position = projectionMatrix * vPosition;
    }
  </script>
  <script type="x-shader/x-fragment" id="fragmentShader">
		precision highp float;

    uniform sampler2D map;

    varying vec2 vUv;
    varying float vScale;

    // HSL to GRB
    vec3 HULtoRGB(float H) {
      H = mod(H, 1.0);
      float R = abs(H * 6.0 - 3.0) - 1.0;
      float G = 2.0 - abs(H * 6.0 - 2.0);
      float B = 2.0 - abs(H * 6.0 - 4.0);
      return clamp(vec3(R,G,B), 0.0, 1.0);
    }

    vec3 HSLtoRGB(vec3 HSL) {
      vec3 RGB = HULtoRGB(HSL.x);
      float C = (1.0 - abs(2.0 * HSL.z - 1.0)) * HSL.y;
      return (RGB - 0.5) * C + HSL.z;
    }

    void main() {
      vec4 deffuseColor = texture2D(map, vUv);
      gl_FragColor = vec4(deffuseColor.xyz * HSLtoRGB(vec3(vScale / 5.0, 1.0, 0.5)), deffuseColor.w);

      if (deffuseColor.w < 0.5) discard;
    }
  </script>
  <script src="./_instancing_billboards.js" type="module"></script>
</body>
</html>